$import('SFBusUtil');
$import('BeanUtil');
var ModelUtil={
	S_refreshMsg:'refresh',
	S_stateSet:'stateSet',
	S_stateCancel:'stateCancel',
	S_Current:'current',
	splitChar:"__"
}

var Model = $createClass('Model',function(param){
	this.name = param.name;
	this.pk = param.pk;
	this.priorSch = param.priorSch;
	this.sortname=param.sortname;
	this.sortorder = param.sortorder;
})
Model.prototype.getCur = function(){
	return this.cur;
}

Model.prototype.setCur = function(pojo){
	if(this.cur){
		this.triggleCurCancelMsg(this.cur)
	}
	this.cur = pojo;
	this.triggleCurSetMsg(this.cur)
	
}

Model.prototype.setCurById = function(id){
	this.setCur(this.getPojoById(id));
}

Model.prototype.triggleCurSetMsg = function(pojo){
	this.triggleStateSetMsg(ModelUtil.S_Current,pojo);
}

Model.prototype.triggleCurCancelMsg = function(pojo){
	this.triggleStateCancelMsg(ModelUtil.S_Current,pojo);
}


Model.prototype.triggleStateSetMsg = function(stateId,pojo){
	this.triggleMsg(
		ModelUtil.S_stateSet,
		{
			stateId:stateId,
			pojo:pojo
		}
	);
}

Model.prototype.triggleStateCancelMsg = function(stateId,pojo){
	this.triggleMsg(
		ModelUtil.S_stateCancel,
		{
			stateId:stateId,
			pojo:pojo
		}
	);
}

Model.prototype.getPojoById = function(id){
	if(!this.datas) return null;
	var datas = this.datas;
	var len = datas.length;
	for(var i=0;i<len;i++){
		if(id==this.getPkValue(datas[i]))
			return datas[i];
	}
	return null;
		
}

Model.prototype.getMsgName = function(){
	return "model"+ModelUtil.splitChar+this.name;
}
Model.prototype.triggleMsg = function(msg,param){
	SFBusUtil.triggerCtrl(this.getMsgName(),msg,param);
}
Model.prototype.refresh = function(){
	this.datas = null;
	this.triggleMsg(ModelUtil.S_refreshMsg);
}

Model.prototype.getPkValue=function(pojo){
	if(pojo)
		return pojo[this.pk];
	return null;
}
Model.prototype.buildId = function(pojo){
	return this.getPkValue(pojo);
}

Model.prototype.list = function(){
	if(!this.datas){
		this.datas = this.getDatasFromSrc();
		if(this.cur){
			this.setCurById(this.getPkValue(this.cur));
		}
	}
	return this.datas;
}

Model.prototype.getDatasFromSrc = function(){
	return this.doSearch(this.getConditions());
}

Model.prototype.search = function(schdata){
	this.schdata = schdata;
	this.refresh();
}

Model.prototype.getSchData = function(){
	return this.schData;
}

Model.prototype.getPageMap = function(){
	if(this.pager)
		return this.pager.getPageMap();
	return {};
	
}
Model.prototype.getSortMap = function(){
	var ret = {};
	if(this.sortname)
		ret.sortname = this.sortname;
	if(this.sortorder)
		ret.sortorder = this.sortorder;
	return ret;
}


Model.prototype.getConditions = function(){
	var ret = {};
	BeanUtil.copyProperty(this.getSchData(),ret);
	BeanUtil.copyProperty(this.getPageMap(),ret);
	BeanUtil.copyProperty(this.getSortMap(),ret);
	BeanUtil.copyProperty(this.priorSch,ret);
	return ret;
}

